package com.ruoyi.luban.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.constant.HttpStatus;
import com.ruoyi.luban.domain.PSchool;
import com.ruoyi.luban.domain.PWxLoginUser;
import com.ruoyi.luban.domain.vo.LoginUserAndTokenVo;
import com.ruoyi.luban.domain.vo.PSchoolVo;
import com.ruoyi.luban.service.IPSchoolService;
import com.ruoyi.luban.service.IPWxLoginUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 学校Controller
 * 
 * @author ruoyi
 * @date 2024-01-28
 */
@RestController
@RequestMapping("/school")
public class PSchoolController extends WXBaseController
{
    @Autowired
    private IPSchoolService pSchoolService;
    @Autowired
    private IPWxLoginUserService wxLoginUserService;
    /**
     * 查询学校列表
     */
    @GetMapping("/list")
    public TableDataInfo list(PSchool pSchool)
    {
        startPage();
        List<PSchool> list = pSchoolService.selectPSchoolList(pSchool);
        return getDataTable(list);
    }


    /**
     * 获取学校详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pSchoolService.selectPSchoolById(id));
    }

    /**
     * 获取坐标内的学校
     * @return
     */
    @GetMapping("getNearbySchools")
    @Log(title = "获取坐标内的学校", businessType = BusinessType.INSERT)
    public AjaxResult getNearbySchools(PSchool pSchool)
    {
        return success(pSchoolService.getNearbySchools(pSchool));
    }


    /**
     * 查询学校列表
     */
    @GetMapping("/listBySign")
    @Log(title = "查询经销商学校列表", businessType = BusinessType.INSERT)
    public TableDataInfo listBySign(PSchool pSchool, @RequestHeader(value = "token",defaultValue = "test")  String token)
    {
        LoginUserAndTokenVo info = this.getLoginUserInfo2(token);
        if (info == null && info.getLoginUser().getLoginUserId() != null){
            TableDataInfo rspData = new TableDataInfo();
            rspData.setCode(HttpStatus.ERROR_LOGIN);
            rspData.setMsg("登录失效");
            return rspData;
//            return error_to_login();
        }
        PWxLoginUser pWxLoginUser = wxLoginUserService.selectPWxLoginUserById(info.getLoginUser().getLoginUserId());
        if (pWxLoginUser == null){
            TableDataInfo rspData = new TableDataInfo();
            rspData.setCode(HttpStatus.FORBIDDEN);
            rspData.setMsg("登录用户不存在");
            return rspData;
        }
        startPage();
        List<PSchoolVo> list = pSchoolService.selectPSchoolListBySign(pWxLoginUser.getContactId());
        return getDataTable(list);
    }

}
